﻿@page "/EventHistory"
@using HardHatCore.HardHatC2Client.Models
@using HardHatCore.HardHatC2Client.Components;

<CascadingAuthenticationState>
<AuthorizeView Roles="Operator,TeamLead,Administrator,Guest">
    <Authorized>
<br>
<h3 class="text-center">EventHistory</h3>
<br>
<MudGrid>
    <MudItem xs="12" sm="5">
        <MudSelect T="TimelineOrientation" ValueChanged="OnOrientationChange" Label="TimelineOrientation" Dense="true" Variant="Variant.Outlined">
            <MudSelectItem T="TimelineOrientation" Value="TimelineOrientation.Vertical">Vertical</MudSelectItem>
            <MudSelectItem T="TimelineOrientation" Value="TimelineOrientation.Horizontal">Horizontal</MudSelectItem>
        </MudSelect>
    </MudItem>
    <MudItem xs="12" sm="5">
        <MudSelect T="TimelinePosition" @bind-Value="@_position" Label="TimelinePosition" Dense="true" Variant="Variant.Outlined">
            @if(_orientation == TimelineOrientation.Vertical)
            {
                <MudSelectItem T="TimelinePosition" Value="TimelinePosition.Start">Start</MudSelectItem>
                <MudSelectItem T="TimelinePosition" Value="TimelinePosition.Left">Left</MudSelectItem>
                <MudSelectItem T="TimelinePosition" Value="TimelinePosition.Alternate">Alternate</MudSelectItem>
                <MudSelectItem T="TimelinePosition" Value="TimelinePosition.Right">Right</MudSelectItem>
                <MudSelectItem T="TimelinePosition" Value="TimelinePosition.End">End</MudSelectItem>
            }
            else if(_orientation == TimelineOrientation.Horizontal)
            {
                <MudSelectItem T="TimelinePosition" Value="TimelinePosition.Top">Top</MudSelectItem>
                <MudSelectItem T="TimelinePosition" Value="TimelinePosition.Alternate">Alternate</MudSelectItem>
                <MudSelectItem T="TimelinePosition" Value="TimelinePosition.Bottom">Bottom</MudSelectItem>
            }
        </MudSelect>
    </MudItem>
    <MudItem xs="12" sm="2">
        <MudSwitch Label="Reverse" @bind-Checked="@_reverse" Disabled="IsSwitchDisabled()" Color="Color.Primary" />
    </MudItem>
</MudGrid>
<hr>
<MudTimeline TimelineOrientation="_orientation" TimelinePosition="_position" Reverse="_reverse">
   @foreach(var item in TimelineEvents)
   {
        <MudTimelineItem Color="@setEventColor(item.Status)" Variant="Variant.Filled">
            <ItemContent>
                <MudAlert Variant="Variant.Outlined" Severity="@setEventSeverity(item.Status)">@item.Event</MudAlert>
                <MudText Class="mud-text-secondary" Color="@setEventColor(item.Status)">@item.Time</MudText>
            </ItemContent>
        </MudTimelineItem>
   }
</MudTimeline>

    </Authorized>
    <NotAuthorized>
        <h1 class="text-center">Not Authorized</h1>
            <UnAuthorizedDialogBox></UnAuthorizedDialogBox>
    </NotAuthorized>
</AuthorizeView>
</CascadingAuthenticationState>

@code {

    private TimelinePosition _position { get; set; } = TimelinePosition.Alternate;
    private TimelineOrientation _orientation { get; set; } = TimelineOrientation.Vertical;
    private bool _reverse { get; set; }

    public static List<HistoryEvent> TimelineEvents = new List<HistoryEvent>();

    //set the eventColor equal to the event Status
    private Color setEventColor(string Status)
    {
        if (Status.Equals("Success",StringComparison.CurrentCultureIgnoreCase))
        {
            return Color.Success;
        }
        else if (Status.Equals("Error",StringComparison.CurrentCultureIgnoreCase))
        {
            return Color.Error;
        }
        else if (Status.Equals("Warning",StringComparison.CurrentCultureIgnoreCase))
        {
            return Color.Warning;
        }
        else if (Status.Equals("Info",StringComparison.CurrentCultureIgnoreCase))
        {
            return Color.Info;
        }
        else
        {
            return Color.Primary;
        }
    }

    //set the eventSeverity based on the event status
    private Severity setEventSeverity(string Status)
    {
        if (Status.Equals("Success",StringComparison.CurrentCultureIgnoreCase))
        {
            return Severity.Success;
        }
        else if (Status.Equals("Error",StringComparison.CurrentCultureIgnoreCase))
        {
            return Severity.Error;
        }
        else if (Status.Equals("Warning",StringComparison.CurrentCultureIgnoreCase))
        {
            return Severity.Warning;
        }
        else if (Status.Equals("Info",StringComparison.CurrentCultureIgnoreCase))
        {
            return Severity.Info;
        }
        else
        {
            return Severity.Normal;
        }
    }

    private void OnOrientationChange(TimelineOrientation value)
    {
        _orientation = value;
        switch (value)
        {
            case TimelineOrientation.Vertical:
                if (_position is TimelinePosition.Top or TimelinePosition.Bottom)
                    _position = TimelinePosition.Start;
                break;
            case TimelineOrientation.Horizontal:
                if (_position is TimelinePosition.Start or TimelinePosition.Left or TimelinePosition.Right or TimelinePosition.End)
                    _position = TimelinePosition.Top;
                break;
        }
    }

    bool IsSwitchDisabled()
    {
        if (_position == TimelinePosition.Alternate)
            return false;
        else
            _reverse = false;
        return true;
    }

    public static async Task AddEvent(HistoryEvent histEvent)
    {
        TimelineEvents.Add(histEvent);
    }

    
}